<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: LanguageMan.php 393 2010-12-25 13:21:42Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalLanguageRecord');
Prado::using('Application.Common.Data.PortalLanguageStringRecord');
class LanguageMan extends PortalAdminPage
{
	public function StringCount($p_culture)
	{
		return $this->DataAccess->createCommand("select count(*) from tbllangstrings where culture='$p_culture'")->queryScalar();
	}

	public function dataBind()
	{
		$this->LanguageList->DataSource	= $this->DataAccess->createCommand("select * from tbllanguages where active=0 order by name")->query();
		$this->LanguageList->dataBind();
		$this->dlLanguageList->DataSource	= $this->DataAccess->createCommand("select * from tbllanguages where active=1 order by displayorder")->query();
		$this->dlLanguageList->dataBind();
	}

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->setAdminPageTitle('Languages');

		if(!$this->IsPostBack)
		{
			$this->dataBind();
		}
	}

	public function AddLanguage($sender, $param)
	{
		if($this->IsValid)
		{
			$id			= $this->LanguageList->Selectedvalue;
			$this->DataAccess->createCommand("update tbllanguages set active=1 where id=$id")->execute();
			$this->dataBind();
		}
	}

	public function deleteItem($sender, $param)
	{
		$id = $this->dlLanguageList->DataKeys[$param->Item->ItemIndex];
		$langRec	= PortalLanguageRecord::finder()->findByid($id);
		$this->DataAccess->createCommand("update tbllanguages set active=0 where id=$id")->execute();
		$this->DataAccess->createCommand("delete from tbllangstrings where culture='" . $langRec->culture . "'")->execute();
		$this->dataBind();
	}

	public function itemCommand($s,$p)
	{
		switch($p->CommandName)
		{
			case "MoveDown":	$this->MoveDown($p->CommandParameter); $this->gotoPage("Admin.LanguageMan"); break;
			case "MoveUp":		$this->MoveUp($p->CommandParameter); $this->gotoPage("Admin.LanguageMan"); break;
			case "import":		$this->gotoPage("Admin.Tools.LanguageImport", array("langid" => $p->CommandParameter)); break;
			case "export":		$this->gotoPage("Admin.Tools.LanguageExport", array("langid" => $p->CommandParameter)); break;
			case "editstrings":	$this->gotoPage("Admin.LanguageEditStrings", array("langid" => $p->CommandParameter)); break;
		}

		$this->dataBind();
	}

	public function MoveUp($id)
	{
		$sql	= "select * from tbllanguages where id=$id";
		$row	= $this->DataAccess->createCommand($sql)->queryRow();

		if($row !== null)
		{
			$sql = "update tbllanguages set displayorder=displayorder -1.001 where id=$id";
			$this->DataAccess->createCommand($sql)->execute();
			$this->RenumDisplayOrder();
			$this->RenumDisplayOrder(1);
		}
	}

	public function MoveDown($id)
	{
		$sql	= "select * from tbllanguages where id=$id";
		$row	= $this->DataAccess->createCommand($sql)->queryRow();

		if($row !== null)
		{
			$sql = "update tbllanguages set displayorder=displayorder +1.001 where id=$id";
			$this->DataAccess->createCommand($sql)->execute();
			$this->RenumDisplayOrder();
			$this->RenumDisplayOrder(1);
		}
	}

	public function RenumDisplayOrder($p_startat = 999.000)
	{
		$newdisplayorder	= $p_startat;
		$sql				= "select * from tbllanguages where active = 1 order by displayorder";
		$rows				= $this->DataAccess->createCommand($sql)->query();

		if($rows)
		{
			foreach($rows as $row)
			{
				$sql = "update tbllanguages set displayorder=$newdisplayorder where id='" . $row["id"] . "'";
				$this->DataAccess->createCommand($sql)->execute();
				$newdisplayorder++;
			}
		}
	}

	public function editItem($sender,$param)
	{
		$this->dlLanguageList->SelectedItemIndex=-1;
		$this->dlLanguageList->EditItemIndex=$param->Item->ItemIndex;
		$this->dataBind();
	}

	public function cancelItem($sender,$param)
	{
		$this->dlLanguageList->SelectedItemIndex=-1;
		$this->dlLanguageList->EditItemIndex=-1;
		$this->dataBind();
	}

	public function updateItem($sender,$param)
	{
		$item=$param->Item;
		$id = $this->dlLanguageList->DataKeys[$item->ItemIndex];
		$active	= $item->edtActive->getChecked();
		$this->DataAccess->createCommand("update tbllanguages set active='$active' where id='$id'")->execute();
		$this->dlLanguageList->EditItemIndex=-1;
		$this->dataBind();
	}

	public function validateLanguage($sender, $param)
	{
		$sql	= "select id from tbllanguages where culture='" . $this->LanguageList->SelectedValue . "'";
		$id		= $this->DataAccess->createCommand($sql)->queryScalar();

		if($id)
		{
			$param->IsValid = false;
		}
		else
		{
			$param->IsValid = true;
		}

		$this->dlLanguageList->DataSource	= $this->DataAccess->createCommand("select * from tbllanguages order by displayorder")->query();
		$this->dlLanguageList->dataBind();
	}

	public function AddString($sender, $param)
	{
		if($this->IsValid)
		{
			// Get Active languages
			$actLang	= PortalLanguageRecord::finder()->findAll("active =?",1);

			if($actLang !== 0 && is_array($actLang) && count($actLang) > 0)
			{
				foreach($actLang as $aLang)
				{
					$lsRec				= new PortalLanguageStringRecord();
					$lsRec->culture		= $aLang->culture;
					$lsRec->lkey		= 'APPLICATION';
					$lsRec->fromvalue	= $this->strFromValue->Text;
                    $lsRec->tovalue		= $this->strToValue->Text;
					$lsRec->save();
				}
			}

			$this->Application->DeleteLanguageCache();
			$this->gotoPage('Admin.LanguageMan');
		}
	}

}
?>